﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Catch - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The Catch statement specifies the code to execute if an exception is raised during execution of a Try statement." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Catch</h1>

<p>Specifies the code to execute if an exception is raised during execution of a <a href="Try.htm">try</a> statement.</p>

<pre class="Syntax" style="line-height: 120%"><span class="func">Catch</span> <span class="optional">OutputVar</span>
    <i>Statement</i></pre>
<pre class="Syntax" style="line-height: 100%"><span class="func">Catch</span> <span class="optional">OutputVar</span>
{
    <i>Statements</i>
}</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>OutputVar</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#variables">变量</a></p>
    <p><em>(Optional)</em> The name of the variable in which to store the value of the exception.</p>
  </dd>

  <dt><i>Statement(s)</i></dt>
  <dd><p>The functions or expressions to execute if an exception is raised.</p></dd>

</dl>

<h2 id="Remarks">备注</h2>
<p><em>OutputVar</em> may optionally be enclosed in parentheses, in which case the space or tab after <code>Catch</code> is optional.</p>
<p>Every use of <em>catch</em> must belong to (be associated with) a <a href="Try.htm">try</a> statement above it. A <em>catch</em> always belongs to the nearest unclaimed <em>try</em> statement above it unless a <a href="Block.htm">block</a> is used to change that behavior.</p>
<p>The <a href="Block.htm#otb">One True Brace (OTB) style</a> may optionally be used. 例如:</p>
<pre>try {
    ...
} catch e {
    ...
}</pre>

<h2 id="RuntimeErrors">Runtime Errors</h2>
<p>A <em>try-catch</em> statement can also be used to handle runtime errors. If a runtime error or exception is not handled, an error message is displayed and the current thread exits. Loadtime errors cannot be handled, since they occur before the <em>try</em> statement is executed.</p>
<p>The value that is stored in <em>OutputVar</em> (if present) is an <a href="Throw.htm#Exception">exception object</a>.</p>

<h2 id="Related">相关</h2>
<p><a href="Try.htm">Try</a>, <a href="Throw.htm">Throw</a>, <a href="Finally.htm">Finally</a>, <a href="Block.htm">Blocks</a>, <a href="OnError.htm">OnError</a></p>

<h2 id="Examples">示例</h2>
<p>See <a href="Try.htm#Examples">Try</a>.</p>

</body>
</html>